//
//  LxMallNotice.m
//  LvXiMark
//
//  Created by apple on 16/4/21.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LxMallNotice.h"

#import "LXMallFrame.h"

#import "NSString+LxString.h"

@interface LxMallNotice()
@property(nonatomic,strong)NSMutableArray * messegeLabels;

@property(nonatomic,strong)NSTimer * timer;

@property(nonatomic,strong)UIImageView * image;

@end
@implementation LxMallNotice

-(void)awakeFromNib{
    [super awakeFromNib];
    self.frameInterval = 1;
    self.layer.masksToBounds = YES;
    self.style = LxMallNoticeStyle1;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timerStop) name:kNoticeStopTimerNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timerStart) name:kNoticeStartTimerNotification object:nil];
}

-(id)initWithFrame:(CGRect)frame frameInterval:(NSInteger)frameInterval{
    if(self = [super initWithFrame:frame]){
        self.frameInterval = frameInterval;
        self.layer.masksToBounds = YES;
        self.style = LxMallNoticeStyle1;
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame frameInterval:(NSTimeInterval)frameInterval style:(LxMallNoticeStyle)style{
    if(self = [super initWithFrame:frame]){
        self.frameInterval = frameInterval;
        self.style = style;
        self.layer.masksToBounds = YES;
    }
    return self;
}

-(void)setMesseges:(NSArray *)messeges{
    _messeges = messeges;
    for (UILabel * label in self.messegeLabels) {
        [label removeFromSuperview];
    }
    [self.messegeLabels removeAllObjects];
    __weak typeof(*&self)ws = self;
    if(self.style == LxMallNoticeStyle1){//上下滚动
        [_messeges enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString * text = obj;
            UILabel * label = [ws labelWith:text frame:CGRectMake(0, idx*self.height, self.width, self.height)];
            [ws.messegeLabels addObject:label];
            [ws addSubview:label];
        }];
    }
    else if(self.style == LxMallNoticeStyle2){//左右滚动
        NSString * final = [_messeges componentsJoinedByString:@" "];
        NSString * fist = [final substringWithRange:NSMakeRange(0, 1)];
        CGSize size = [fist sizeInAre:CGSizeMake(MAXFLOAT, MAXFLOAT) fontsize:10];
        
        for (int i = 0; i<final.length; i++) {
            NSString * st = [final substringWithRange:NSMakeRange(i, 1)];
            UILabel * label = [self labelWith:st frame:CGRectMake(i*size.width, 0, size.width, self.height)];
            [ws.messegeLabels addObject:label];
            [self addSubview:label];
        }
    }
    
}


-(UILabel*)labelWith:(NSString*)text frame:(CGRect)frame{
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textAlignment = NSTextAlignmentLeft;
    label.numberOfLines = 0;
    label.textColor = [UIColor orangeColor];
    label.font = [UIFont systemFontOfSize:12];
    return label;
}

-(void)handleTimer{
    __weak typeof(*&self)ws = self;
    __block NSInteger count = self.messegeLabels.count;
    
    switch (self.style) {
        case LxMallNoticeStyle1:{//上下滚动
            for (NSInteger i = 0 ; i<count; i++) {
                __block  UILabel * label = self.messegeLabels[i];
               
                [UIView animateWithDuration:0.5 animations:^{
                    label.centerY = label.centerY-ws.height;
                }completion:^(BOOL finished) {
                    if(label.centerY<-label.height*(count-2)){
                        label.centerY = ws.height*1.5;
                    }
                    
                    if(label.centerY == self.height/2.0){
                         self.currentMessege = label.text;
                    }
                }];
            }
        }break;
        case LxMallNoticeStyle2:{//左右滚动
            for (int i = 0 ; i<count; i++) {
                 __block  UILabel * label = self.messegeLabels[i];
                [UIView animateWithDuration:ws.frameInterval*1.5 animations:^{
                    label.centerX = label.centerX-label.width;
                }completion:^(BOOL finished) {
                    if(label.x<-label.width){
                        label.x = (count-1)*label.width;
                    }
                }];
            }
        }break;
        default:
            break;
    }
}

-(NSMutableArray*)messegeLabels{
    if(_messegeLabels==nil){
        _messegeLabels = [[NSMutableArray alloc] init];
    }
    return _messegeLabels;
}




-(void)timerStart{
    [self.timer invalidate];
    self.timer = nil;
    if(self.timer==nil){
        self.frameInterval = self.frameInterval == 0?1:self.frameInterval;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:self.frameInterval target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    [self.messegeLabels enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel * lab = obj;
        lab.y = idx*self.height;
    }];
}

-(void)timerStop{
    [self.timer invalidate];
    self.timer = nil;
}

-(NSTimer*)timer{
    if(_timer==nil){
        self.frameInterval = self.frameInterval == 0?1:self.frameInterval;
        _timer = [NSTimer scheduledTimerWithTimeInterval:self.frameInterval target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
        [self timerStop];
        
    }
    return _timer;
}


-(UIImageView*)image{
    if(_image==nil){
        _image = [[UIImageView alloc] init];
    }
    return _image;
}



-(void)setMessegeFont:(UIFont *)messegeFont{
    _messegeFont = messegeFont;
    for (UILabel * label in self.messegeLabels) {
        label.font = _messegeFont;
    }
}

-(void)setMessegeColor:(UIColor *)messegeColor{
    _messegeColor = messegeColor;
    for (UILabel * label in self.messegeLabels) {
        label.textColor = _messegeColor;
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_timer invalidate];
    _timer = nil;
    _image = nil;
}


@end

NSString * kNoticeStopTimerNotification = @"NoticeStopTimerNotification";

NSString * kNoticeStartTimerNotification = @"NoticeStartTimerNotification";
